local boquanchen = fk.CreateSkill {
    name = "boquanchen",
}

Fk:loadTranslationTable({
    ["boquanchen"] = "权臣",
    [":boquanchen"] = "出牌阶段限一次，你可以令所有其他存活角色弃牌至手牌数小于你的手牌数（X为你的手牌数）。若有角色未弃牌，你对其造成3点伤害。",
    ["#boquanchen"] = "是否发动权臣？", -- 询问提示
    ["$boquanchen1"] = "尔欲行大事，问过吾掌中兵刃否？",
    ["$boquanchen2"] = "西凉铁骑曳城，天下高楼可摧！",
})

boquanchen:addEffect("active", {
    anim_type = "discard",
    prompt = "#boquanchen", -- 技能发动提示
    card_num = 0,
    target_num = 0,

    -- 触发条件（出牌阶段+手牌数+存活角色）
    can_use = function(self, player)
        return 
            player:usedSkillTimes(boquanchen.name, Player.PlayPhase) == 0 and -- 出牌阶段（修正阶段）
            player:getHandcardNum() > 0 and 
            #Fk:currentRoom().alive_players > 1
    end,

    -- 询问流程（模仿模板的on_cost逻辑）
    on_cost = function(self, event, target, player, data)
        local room = player.room
        -- 询问是否发动技能
        local answer = room:askToSkillInvoke(player, {
            skill_name = boquanchen.name,
            prompt = "#boquanchen",
            cancelable = true -- 允许取消
        })
        if answer then
            -- 标记技能已使用（避免重复发动）
            player:addSkillUse(boquanchen.name, Player.PlayPhase)
            return true
        else
            -- 取消时清除标记（关键修复：允许再次发动）
            player:removeSkillUse(boquanchen.name, Player.PlayPhase)
            return false
        end
    end,

    -- 技能执行逻辑
    on_use = function(self, room, effect)
        local player = effect.from
        local x = player:getHandcardNum() or 0
        local tos = room:getOtherPlayers(player)
        
        -- 显示指示线
        room:doIndicate(player.id, table.map(tos, Util.IdMapper))
        
        -- 处理存活角色弃牌
        for _, target in ipairs(tos) do
            if target:isAlive() then
                local need_discard = math.max(target:getHandcardNum() - (x - 1), 0)
                local original_handcard_num = target:getHandcardNum()
                if need_discard > 0 then
                    -- 询问目标角色弃牌
                    local result = room:askToDiscard(target, {
                        player = target,
                        min_num = need_discard,
                        max_num = need_discard,
                        include_equip = false,
                        skill_name = boquanchen.name,
                        cancelable = true,
                        prompt = "#boquanchen_discard::" .. (x - 1)
                    })
                    if result then
                        -- 可以在这里添加弃牌成功后的逻辑
                    end
                end
                -- 检查是否未弃牌
                if target:getHandcardNum() == original_handcard_num then
                    room:damage {
                        from = player,
                        to = target,
                        damage = 3,
                        skillName = boquanchen.name,
                    }
                end
            end
        end
        
        -- 发送战报
        room:sendLog{
            type = "#InvokeSkill",
            from = player.id,
            arg = boquanchen.name,
        }
    end,
})

return boquanchen